package com.remoteapp.activities;

import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

import com.remoteapp.config.AppConfig;
import com.remoteapp.connection.ConnectionHandler;
import com.remoteapp.main.R;

public class ConnectionActivity extends Activity implements OnClickListener,
		OnCheckedChangeListener {

	/** Button to connect with the configured settings */
	Button buttonConnect;

	/** EditText for the connection settings */
	EditText editIP;
	EditText editPort;

	/** CheckBox to check auto connect on app start */
	CheckBox checkAutoConnect;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_connection);

		editIP = (EditText) findViewById(R.id.editIP);
		editPort = (EditText) findViewById(R.id.editPort);
		buttonConnect = (Button) findViewById(R.id.buttonConnect);
		checkAutoConnect = (CheckBox) findViewById(R.id.checkAutoConnect);

		// Get values from configuration
		editIP.setText(AppConfig.IP_ADDRESS);
		checkAutoConnect.setChecked(AppConfig.AUTO_CONNECT);
		if (AppConfig.PORT >= 0) {
			editPort.setText(String.valueOf(AppConfig.PORT));
		}

		// Set listeners
		buttonConnect.setOnClickListener(this);
		checkAutoConnect.setOnCheckedChangeListener(this);
		
		// Set button text
		if (ConnectionHandler.isConnected()) {
			buttonConnect.setText(getString(R.string.BUTTON_DISCONNECT));
		} else {
			buttonConnect.setText(getString(R.string.BUTTON_CONNECT));
		}
	}

	@Override
	protected void onDestroy() {
		try {
			String ip = editIP.getText().toString();
			String port = editPort.getText().toString();
			
			// Save IP
			if (!ip.isEmpty()) {
				AppConfig.IP_ADDRESS = ip;
			}
			// Save Port
			if (!port.isEmpty()) {
				AppConfig.PORT = Integer.parseInt(editPort.getText().toString());
			}
			
			AppConfig.saveConfig();

		} catch (IOException e) {
			Toast.makeText(getApplicationContext(), e.getMessage(),
					Toast.LENGTH_LONG).show();
		}

		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.connection, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		try {

			if (v == buttonConnect) {

				if (!ConnectionHandler.isConnected()) {
					String ipAddr = editIP.getText().toString();
					int port = Integer.parseInt(editPort.getText().toString());

					// Connect to server with given ID
					ConnectionHandler.connectClientToServer(ipAddr, port);

				} else {
					// Disconnect from server
					ConnectionHandler.disconnectClientFromServer();
				}
				
				// Check if connected or disconnected
				if (ConnectionHandler.isConnected()) {
					buttonConnect.setText(getString(R.string.BUTTON_DISCONNECT));
				} else {
					buttonConnect.setText(getString(R.string.BUTTON_CONNECT));
				}
			}
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), e.getMessage(),
					Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (buttonView == checkAutoConnect) {
			AppConfig.AUTO_CONNECT = isChecked;
		}
	}

}
